package com.ml.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.DisplayMetrics;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.marswin89.marsdaemon.DaemonClient;
import com.marswin89.marsdaemon.DaemonConfigurations;
import com.ml.tools.Config;
import com.ml.tools.Utils;
import com.ml.view.Receiver1;
import com.ml.view.Receiver2;
import com.ml.view.Service1;
import com.ml.view.Service2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

public class MyApplication extends Application {
    public static String SERVERPATH = Config.SERVER_PATH;
    public static String qx;
    public static List<Activity> mylist = new LinkedList<Activity>();
    public static MyApplication instance;
    public static HashMap<String, String> map;
    public static String yy;
    public static Bitmap bit = null;
    public static List<Integer> lli = new ArrayList<Integer>();
    public static String linename, dqclstr, dclstr, pphstr, sn, urls, taitou, riqi, name, name2;
    public static long xtime = 0; // 一体机时间 - 本机时间    秒     差距<60秒 不用设置
    static SharedPreferences sharedUserInfo;
    static SharedPreferences serversInfo;
    private static MyApplication mInstance = null;
    private static final List<Activity> mList = new LinkedList<Activity>();
    private static RequestQueue mRequestQueue;
    private String ip = "";
    private String port = "";

    public static RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getInstance());
        }
        return mRequestQueue;
    }

    public static MyApplication getInstance() {
        return mInstance;
    }

    public static void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    public static SharedPreferences getSharedUserInfo() {
        if (sharedUserInfo == null) {
            sharedUserInfo = getInstance().getSharedPreferences("test", Context.MODE_PRIVATE);
        }
        return sharedUserInfo;
    }

    public static SharedPreferences getServersInfo() {
        if (serversInfo == null) {
            serversInfo = getInstance().getSharedPreferences("servers", Context.MODE_PRIVATE);
        }
        return serversInfo;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        ip = MyApplication.getServersInfo().getString("serverip", "");
        port = MyApplication.getServersInfo().getString("serverport", "");
        qx = MyApplication.getSharedUserInfo().getString("qx", "");
        MyApplication.SERVERPATH = "http://" + ip + ":" + port;
        instance = this;
        MyApplication.yy = MyApplication.getSharedUserInfo().getString("language", "zh");
        map = Utils.setString();
    }

    // add Activity
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    public void changeAppLanguage(Locale locale) {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        Configuration configuration = getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        getResources().updateConfiguration(configuration, metrics);
    }

    //进程常驻
    private DaemonClient mDaemonClient;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        mDaemonClient = new DaemonClient(createDaemonConfigurations());
        mDaemonClient.onAttachBaseContext(base);
    }

    private DaemonConfigurations createDaemonConfigurations() {
        DaemonConfigurations.DaemonConfiguration configuration1 = new DaemonConfigurations.DaemonConfiguration(
                "com.marswin89.marsdaemon.demo:process1",
                Service1.class.getCanonicalName(),
                Receiver1.class.getCanonicalName());
        DaemonConfigurations.DaemonConfiguration configuration2 = new DaemonConfigurations.DaemonConfiguration(
                "com.marswin89.marsdaemon.demo:process2",
                Service2.class.getCanonicalName(),
                Receiver2.class.getCanonicalName());
        DaemonConfigurations.DaemonListener listener = new MyDaemonListener();
        //return new DaemonConfigurations(configuration1, configuration2);//listener can be null
        return new DaemonConfigurations(configuration1, configuration2, listener);
    }


    class MyDaemonListener implements DaemonConfigurations.DaemonListener {
        @Override
        public void onPersistentStart(Context context) {
        }

        @Override
        public void onDaemonAssistantStart(Context context) {
        }

        @Override
        public void onWatchDaemonDaed() {
        }
    }

}
